//
//  Exam_popView.m
//  penzer
//
//  Created by caiyc on 2018/10/1.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "Exam_popView.h"
#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
#define ALERT_HTIGHT 400
@interface Exam_popView()<UIGestureRecognizerDelegate>
@property (nonatomic,strong) UIView *alertview;
@property (nonatomic,strong)UILabel *titleLab;
@property (nonatomic,strong)UILabel *messageLab;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *otherBtn;
@property(nonatomic,strong)UIImageView *backImg;
@property(nonatomic,strong)UIImageView *left_Img;
@property(nonatomic,strong)UIImageView *right_Img;
@property(nonatomic,strong)UIButton *left_Btn;
@property(nonatomic,strong)UIButton *right_Btn;
@property(nonatomic,strong)UIView *examView;
@property(nonatomic,strong)UIButton *cofirm_Btn;
@property(nonatomic,strong)UIButton *close_Btn;
@property(assign)NSInteger selectAns;
@property(nonatomic,strong)NSMutableArray *Btn_Arr;
@property(nonatomic,strong)NSDictionary *exam_Dic;


@end
@implementation Exam_popView
-(NSMutableArray*)Btn_Arr{
    if(!_Btn_Arr){
        _Btn_Arr = [NSMutableArray array];
    }
    return _Btn_Arr;
}
-(instancetype)initWithexamdata:(NSDictionary *)dic{
    self = [super init];
    if(self){
        
        NSArray *arr = dic[@"game_items"];
        _exam_Dic = dic;
     //   NSDictionary *diction= @{@"title":@"中国共产党成立于哪年？",@"answer":arr};
        self.frame = [UIScreen mainScreen].bounds;
        self.backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 480)];
        self.backImg.image = [UIImage imageNamed:@"Group 5"];
        //self.backImg.contentMode = UIViewContentModeScaleAspectFit;
        self.alertview.frame = CGRectMake(0, screen_height-480, SCREEN_WIDTH, 480);
        [self.alertview addSubview:self.backImg];
        //  self.alertview.center = self.center;
        [self addSubview:self.alertview];
        
        
        self.examView = [[UIView alloc]initWithFrame:CGRectMake(30, 130, screen_width-60, 280)];
        self.examView.backgroundColor = [UIColor whiteColor];
        self.examView.layer.cornerRadius = 5;
        self.examView.layer.masksToBounds = 1;
        UIScrollView *scroView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screen_width-60, 280)];
        NSString *titeStr = dic[@"question"];
        CGSize sizeT = [XTool caculatorStringHeigh:titeStr width:screen_width-80 CGfloat:16];
        UILabel *titles = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, screen_width-80, sizeT.height)];
        titles.text = titeStr;
        titles.textAlignment = NSTextAlignmentCenter;
        titles.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
        [self.examView addSubview:scroView];
        [scroView addSubview:titles];
        CGFloat hieghtAns = 30+sizeT.height;
        for(int i =0;i<arr.count;i++){
            UIButton *ans = [UIButton buttonWithType:UIButtonTypeCustom];
            ans.frame = CGRectMake(10, hieghtAns+50*i, screen_width-80, 40);
            //ans.text = arr[i];
            NSString *title = [NSString stringWithFormat:@"%@：%@",arr[i][@"li"],arr[i][@"opt"]];
            [ans setTitle:title forState:UIControlStateNormal];
             [ans setTitleEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
            ans.layer.cornerRadius = 20;
            ans.layer.masksToBounds = 1;
            [ans setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            ans.backgroundColor = viewcontrollerColor;
            
            ans.titleLabel. font = [UIFont fontWithName:@"Helvetica-Light" size:14];
            [scroView addSubview:ans];
            ans.tag = i;
            [self.Btn_Arr addObject:ans];
            [ans addTarget:self action:@selector(selectAnss:) forControlEvents:UIControlEventTouchUpInside];
        }
        scroView.contentSize = CGSizeMake(0, 50*arr.count+10+sizeT.height);
        [self.alertview addSubview:self.examView];
        
        self.cofirm_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cofirm_Btn.frame = CGRectMake(screen_width/2-100, 420, 200, 50);
        self.cofirm_Btn.backgroundColor = [UIColor whiteColor];
        self.cofirm_Btn.layer.cornerRadius = 25;
        self.cofirm_Btn.layer.masksToBounds = 1;
        [self.cofirm_Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.cofirm_Btn setTitle:@"提交答案" forState:UIControlStateNormal];
        [self.alertview addSubview:self.cofirm_Btn];
        [self.cofirm_Btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        self.close_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.close_Btn.frame = CGRectMake(screen_width-60, 10, 50, 30);
        [self.close_Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.close_Btn setTitle:@"收起" forState:UIControlStateNormal];
        self.close_Btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.alertview addSubview:self.close_Btn];
        [self.close_Btn addTarget:self action:@selector(dismissAlertView) forControlEvents:UIControlEventTouchUpInside];
        
        

    }
    return self;
}
-(void)selectAnss:(UIButton *)sender{
    self.selectAns = sender.tag;
    sender.backgroundColor = BUTTON_COLOR;
    for(UIButton *bnr in self.Btn_Arr){
        if(bnr.tag!=sender.tag){
            bnr.backgroundColor = viewcontrollerColor;
        }
    }
}
//-(instancetype)init{
//    self = [super init];
//    if(self){
//
//
//
//        self.frame = [UIScreen mainScreen].bounds;
//        self.backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 480)];
//        self.backImg.image = [UIImage imageNamed:@"Group 5"];
//        //self.backImg.contentMode = UIViewContentModeScaleAspectFit;
//        self.alertview.frame = CGRectMake(0, screen_height-480, SCREEN_WIDTH, 480);
//        [self.alertview addSubview:self.backImg];
//      //  self.alertview.center = self.center;
//        [self addSubview:self.alertview];
//
//
//        self.examView = [[UIView alloc]initWithFrame:CGRectMake(30, 130, screen_width-60, 280)];
//        self.examView.backgroundColor = [UIColor whiteColor];
//        self.examView.layer.cornerRadius = 5;
//        self.examView.layer.masksToBounds = 1;
//
//        [self.alertview addSubview:self.examView];
//
//        self.cofirm_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        self.cofirm_Btn.frame = CGRectMake(screen_width/2-100, 420, 200, 50);
//        self.cofirm_Btn.backgroundColor = [UIColor whiteColor];
//        self.cofirm_Btn.layer.cornerRadius = 25;
//        self.cofirm_Btn.layer.masksToBounds = 1;
//        [self.cofirm_Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [self.cofirm_Btn setTitle:@"提交答案" forState:UIControlStateNormal];
//        [self.alertview addSubview:self.cofirm_Btn];
//        [self.cofirm_Btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//
//        self.close_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        self.close_Btn.frame = CGRectMake(screen_width-60, 10, 50, 30);
//        [self.close_Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [self.close_Btn setTitle:@"收起" forState:UIControlStateNormal];
//        self.close_Btn.titleLabel.font = [UIFont systemFontOfSize:13];
//        [self.alertview addSubview:self.close_Btn];
//        [self.close_Btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//
//
//    }
//    return self;
//
//}
-(UIView *)alertview
{
    if (_alertview == nil) {
        _alertview = [[UIView alloc] init];
        _alertview.backgroundColor = [UIColor clearColor];
        _alertview.layer.cornerRadius=5.0;
        _alertview.layer.masksToBounds=YES;
        _alertview.userInteractionEnabled=YES;
    }
    return _alertview;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
}
-(void)show
{
    self.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
    
    self.alertview.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.2,0.2);
    self.alertview.alpha = 0;
    [UIView animateWithDuration:0.3 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.4f];
        self.alertview.transform = transform;
        self.alertview.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}
-(void)btnClick:(UIButton *)btn{
    NSString *right = self.exam_Dic[@"right_answer"];
    NSArray *item_Arr = self.exam_Dic[@"game_items"];
    BOOL isrgh = 0;
    if([item_Arr[self.selectAns][@"li"]isEqualToString:right]){
        isrgh = 1;
    }
    //    if ([self.delegate respondsToSelector:@selector(AlertView:clickedButtonAtIndex:)]) {
    //        [self.delegate dxAlertView:self clickedButtonAtIndex:btn.tag];
    //    }
//    if([self.delegate respondsToSelector:@selector(AlertView:clickedButtonAtIndex:)]){
//        [self.delegate AlertView:self clickedButtonAtIndex:btn.tag];
//    }
    if([self.delegate respondsToSelector:@selector(AlertView:clickedButtonAtIndex:andans:andright:)]){
        [self.delegate AlertView:self clickedButtonAtIndex:btn.tag andans:self.selectAns andright:isrgh];
    }
    [self dismissAlertView];
}
-(void)dismissAlertView{
    [UIView animateWithDuration:0.3 animations:^{
        [self removeFromSuperview];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
